package com.edu.web.servlet;

import com.alibaba.fastjson2.JSON;
import com.edu.entity.PageBean;
import com.edu.entity.Vehicles;
import com.edu.service.VehiclesService;
import com.edu.service.imlp.VehiclesServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/selectVehiclesServlet")
public class SelectVehiclesServlet extends HttpServlet {
    VehiclesService vehiclesService = new VehiclesServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*String type = "跑车";
        int page = 0;
        int pageSize = 5;*/
        String type = req.getParameter("type") == null? "跑车":req.getParameter("type") ;
        int page = Integer.parseInt(req.getParameter("page") == null ? "1" : req.getParameter("page"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize") == null ? "5" : req.getParameter("pageSize"));
        PageBean<Vehicles> pageBean = vehiclesService.queryByType(type, page, pageSize);
        String jsonString = JSON.toJSONString(pageBean);
        PrintWriter writer = resp.getWriter();
        writer.print(jsonString);
        writer.flush();
        writer.close();

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
